home *** CD-ROM | disk | FTP | other *** search
/ Acorn RISC PD-CD 1 / Acorn RISC PD-CD 1.iso / languages / _tile / test / locals < prev    next >
Encoding:
Text File  |  1991-08-10  |  743 b   |  56 lines

  1. .( Loading Locals test...) cr
  2.  
  3. locals
  4.  
  5. .( 1: Redefinition of the basic stack operations using argument binding) cr
  6.  
  7. : swap { a b } b a ;
  8. : dup { a } a a ;
  9. : drop { a } ;
  10. : rot { a b c } b c a ;
  11.  
  12. 1 2 .s swap cr
  13. 3   .s dup cr
  14.     .s drop cr
  15.     .s rot cr
  16.     .s cr
  17. drop drop drop 
  18.  
  19.  
  20. .( 2: Recursive factorial function with argument binding) cr
  21.  
  22. : recursive { n }
  23.   n 0>
  24.   if n 1- recurse n *
  25.   else 1 then
  26. ;
  27.  
  28. 5 recursive . cr
  29.  
  30.  
  31. .( 3: Tail recursive factorial function) cr
  32.  
  33. : tail-recursive { n a }
  34.   n 0>
  35.   if n 1- n a * tail-recurse
  36.   else a then
  37. ;
  38.     
  39. 5 1 tail-recursive . cr
  40.  
  41.  
  42. .( 4: Iterative factorial function with a local variable) cr
  43.  
  44. : iterative { n | a }
  45.   1 -> a
  46.   n 1+ 1 do
  47.     i a * -> a
  48.   loop
  49.   a
  50. ;
  51.  
  52. 5 iterative . cr
  53.  
  54. forth only
  55.  
  56.